package com.ripservice.controller;

import com.ripservice.dto.RouteSnapshotDto;
import com.ripservice.entity.RouteSnapshotEntity;
import com.ripservice.service.SnapshotService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 快照控制器
 * 提供路由快照的查询和删除接口
 */
@RestController
@RequestMapping("/api/snapshots")
@RequiredArgsConstructor
public class SnapshotController {

    private final SnapshotService snapshotService;

    /**
     * 根据模拟ID获取所有路由快照
     *
     * @param simulationId 模拟ID
     * @return ResponseEntity 包含路由快照列表的响应实体
     */
    @GetMapping("/simulation/{simulationId}")
    public ResponseEntity<List<RouteSnapshotDto>> getSnapshotsBySimulation(@PathVariable("simulationId") Long simulationId) {
        List<RouteSnapshotEntity> entities = snapshotService.getSnapshotsBySimulation(simulationId);
        List<RouteSnapshotDto> dtos = entities.stream()
                .map(RouteSnapshotDto::new)
                .collect(Collectors.toList());
        return ResponseEntity.ok(dtos);
    }

    /**
     * 根据拓扑ID获取所有路由快照
     *
     * @param topologyId 拓扑ID
     * @return ResponseEntity 包含路由快照列表的响应实体
     */
    @GetMapping("/topology/{topologyId}")
    public ResponseEntity<List<RouteSnapshotDto>> getSnapshotsByTopology(@PathVariable("topologyId") Long topologyId) {
        List<RouteSnapshotEntity> entities = snapshotService.getSnapshotsByTopology(topologyId);
        List<RouteSnapshotDto> dtos = entities.stream()
                .map(RouteSnapshotDto::new)
                .collect(Collectors.toList());
        return ResponseEntity.ok(dtos);
    }

    /**
     * 获取指定路由器的历史快照
     *
     * @param topologyId 拓扑ID
     * @param routerId  路由器ID
     * @return ResponseEntity 包含路由快照列表的响应实体
     */
    @GetMapping("/topology/{topologyId}/router/{routerId}")
    public ResponseEntity<List<RouteSnapshotDto>> getSnapshotsByRouter(
            @PathVariable("topologyId") Long topologyId,
            @PathVariable("routerId") String routerId) {
        List<RouteSnapshotEntity> entities = snapshotService.getSnapshotsByRouter(topologyId, routerId);
        List<RouteSnapshotDto> dtos = entities.stream()
                .map(RouteSnapshotDto::new)
                .collect(Collectors.toList());
        return ResponseEntity.ok(dtos);
    }

    /**
     * 获取指定路由器的最新快照
     *
     * @param topologyId 拓扑ID
     * @param routerId  路由器ID
     * @return ResponseEntity 包含最新路由快照的响应实体，如果不存在则返回404
     */
    @GetMapping("/topology/{topologyId}/router/{routerId}/latest")
    public ResponseEntity<RouteSnapshotDto> getLatestSnapshotByRouter(
            @PathVariable("topologyId") Long topologyId,
            @PathVariable("routerId") String routerId) {
        RouteSnapshotEntity entity = snapshotService.getLatestSnapshotByRouter(topologyId, routerId);
        if (entity == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(new RouteSnapshotDto(entity));
    }

    /**
     * 删除指定仿真的所有历史快照
     *
     * @param simulationId 模拟ID
     * @return ResponseEntity 表示操作成功的响应实体
     */
    @DeleteMapping("/simulation/{simulationId}")
    public ResponseEntity<Void> deleteSnapshotsBySimulation(@PathVariable("simulationId") Long simulationId) {
        snapshotService.deleteSnapshotsBySimulation(simulationId);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除指定拓扑的所有历史快照
     *
     * @param topologyId 拓扑ID
     * @return ResponseEntity 表示操作成功的响应实体
     */
    @DeleteMapping("/topology/{topologyId}")
    public ResponseEntity<Void> deleteSnapshotsByTopology(@PathVariable("topologyId") Long topologyId) {
        snapshotService.deleteSnapshotsByTopology(topologyId);
        return ResponseEntity.ok().build();
    }

    /**
     * 组合查询路由快照
     *
     * @param topologyId 拓扑ID（可选）
     * @param simulationId 仿真ID（可选）
     * @param routerId 路由器ID（可选）
     * @return ResponseEntity 包含路由快照列表的响应实体
     */
    @GetMapping("/filter")
    public ResponseEntity<List<RouteSnapshotDto>> getSnapshotsByFilter(
            @RequestParam(value = "topologyId", required = false) Long topologyId,
            @RequestParam(value = "simulationId", required = false) Long simulationId,
            @RequestParam(value = "routerId", required = false) String routerId) {

        List<RouteSnapshotEntity> entities = snapshotService.getSnapshotsByFilter(
                topologyId, simulationId, routerId);

        List<RouteSnapshotDto> dtos = entities.stream()
                .map(RouteSnapshotDto::new)
                .collect(Collectors.toList());

        return ResponseEntity.ok(dtos);
    }
}
